//
//  VideoPlayViewController.m
//  HaHa
//
//  Created by 元虎的mac on 15/9/22.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "VideoPlayViewController.h"

@interface VideoPlayViewController ()
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *recentCommentsArray;


@property (nonatomic,strong) UIActivityIndicatorView *activity;
@property (nonatomic,strong) CommentHeaderView *headerView;
@end

@implementation VideoPlayViewController
{
    //标示当前评论的总量 已经请求
    NSInteger number;
}

- (void)viewDidLoad {
    self.tabBarController.tabBar.hidden = YES;
    number = 0;
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [super viewDidLoad];
    self.title = @"详情";

    [self addTableView];
    [self AddAcTivityView];
    [self addHeaderView];
    [self GetData];
    __block VideoPlayViewController *root = self;
    [self.tableView addHeaderWithCallback:^{
        number -= 20;
        root.recentCommentsArray = [NSMutableArray array];
        [root GetData];
    [root.tableView headerEndRefreshing];
    }];
    [self.tableView addFooterWithCallback:^{
        [root GetData];
    [root.tableView footerEndRefreshing];
    }];
    
}


- (void)BackView{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 添加表视图
- (void)addTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 0, kScreenWdth-10, kScreenHeight-49)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
#pragma mark - 添加表头
- (void)addHeaderView{
    NSInteger videoWdith = [_Mode.video_width integerValue];
    CGFloat returnVideoHeight = [_Mode.video_height integerValue];
    if (videoWdith > self.tableView.width) {
        //计算表视图的宽度与视频宽度的比例
        CGFloat beishu = videoWdith / self.tableView.width;
        
        returnVideoHeight = [_Mode.video_height integerValue] / beishu;
    }
    if (returnVideoHeight == 0) {
        returnVideoHeight = kScreenWdth-30;
    }
    NSLog(@"%f",returnVideoHeight);
    
    _headerView = [[CommentHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWdth,102+returnVideoHeight)];
    self.tableView.tableHeaderView = _headerView;
    
    
    [_headerView SetContentWithMode:_Mode];
    [_headerView PlayWithUrlStr:_Mode.mp4_url];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (self.recentCommentsArray.count==0) {
        return @"暂无评论";
    }
    return @"最新评论";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}
#pragma mark - 风火轮
- (void)AddAcTivityView{
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activity.alpha = 0.9;
    self.activity.frame = CGRectMake(self.tableView.width/2-40, kScreenHeight-200, 80, 80);
    self.activity.layer.cornerRadius = 5;
    self.activity.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.activity.layer.borderWidth = 1;
    [self.activity startAnimating];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 80, 20)];
    label.font = [UIFont boldSystemFontOfSize:11];
    label.text = @"正在加载..";
    label.textAlignment = NSTextAlignmentCenter;
    _activity.backgroundColor = [UIColor grayColor];
    [_activity addSubview:label];
    [self.view addSubview:_activity];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.recentCommentsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.recentCommentsArray.count!=0) {
        commentsMode *Mode = self.recentCommentsArray[indexPath.row];
        CGFloat textHeight = [self YYHreturnStringHeight:Mode.commentContent Width:kScreenWdth-65 Font:17];
        return 60+textHeight;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    commentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[commentsTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    if (self.recentCommentsArray.count!=0) {
        commentsMode *mode = self.recentCommentsArray[indexPath.row];
        [cell setContentWithMode:mode];

    }
        return cell;
}



//数据解析
- (void)GetData{
    [self.activity startAnimating];
    if (!self.recentCommentsArray) {
        self.recentCommentsArray = [NSMutableArray array];
    }
    number+=20;
    NSString *url = [NSString stringWithFormat:@"%@%ld%@%@%@",KcommentA,number,KcommentB,_Mode.group_id,KcommentC];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *Dic = (NSDictionary *)response;
        NSDictionary *dataDic = [Dic objectForKey:@"data"];
        NSArray *dataArray = [dataDic objectForKey:@"recent_comments"];
        int a = 0;
        for (NSDictionary *temp in dataArray) {
            ++a;
            if (a>self.recentCommentsArray.count) {
                commentsMode *mode = [commentsMode setHModeWithDic:temp];
                [self.recentCommentsArray addObject:mode];
            }
        }
        [self.tableView reloadData];
        [self.activity stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

//计算高度
- (CGFloat)YYHreturnStringHeight:(NSString *)string Width:(CGFloat)Width Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
